#!/usr/bin/env coffee

import {join} from 'path'
import {app,screen,BrowserWindow,globalShortcut} from 'electron'


toggleDevTools = (win)=>
  if win
    if not win.devToolsWebContents
      {webContents} = win
      devtools = new BrowserWindow(
        minimizable:false
        maximizable:false
      )

      [w,h] = win.getSize()
      [x,y] = win.getPosition()
      {width,height} = screen.getPrimaryDisplay().workArea

      webContents.setDevToolsWebContents(devtools.webContents)
      webContents.openDevTools mode: 'detach'

      if w > h
        y = 0
        w = 0
        h += 20
        height -= h
      else
        x = 0
        h = 0
        width -= w


      devtools.setPosition w,h
      win.setPosition x,y

      devtools.setSize width,height

      win.focus()

      ###
      fp = join(
        app.getPath('userData')
        'Preferences'
      )
      config = JSON.parse readFileSync(fp, 'utf8')
      config.electron.devtools.preferences[
        'InspectorView.splitViewState'
      ] = JSON.stringify(
        vertical: { size: width - app.size[0] }
      )
      writeFileSync(fp, JSON.stringify(config))
      ###



  return


export default =>
  globalShortcut.register 'CmdOrCtrl+Alt+I', =>
    toggleDevTools BrowserWindow.getFocusedWindow()

  return
